package com.lancea12.buster.android;

import java.util.HashMap;

import org.json.JSONObject;

import android.app.Activity;
import android.content.res.Configuration;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.DragEvent;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class ControllerActivity extends Activity {

	private Button finishButton;
	private Button leftDriveButton;
	private Button leftReverseButton;
	private Button rightDriveButton;
	private Button rightReverseButton;
	private ControllerEndpoint endpoint;
	private Thread eThread;
	private Handler mHandler;
	private TextView responseView;
	private VideoView videoView;
	private GestureDetector gd;
	
	private enum Direction {
		UP("up"),
		DOWN("down"),
		LEFT("left"),
		RIGHT("right");
		
		private String name;
		
		Direction(String name){
			this.name = name;
		}
		
		String getName(){
			return name;
		}
	}
	
	private class NavigationOnGestureListener extends SimpleOnGestureListener{
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {

			responseView.setText("drag x= " + distanceX + " y = " + distanceY);
			look(distanceX, distanceY);
			
			return super.onScroll(e1, e2, distanceX, distanceY);
		}
	}
	
	private void setupTopLayout(){
		LinearLayout tempLL = (LinearLayout)findViewById(R.id.controllerTopLayout);
		Configuration c = getResources().getConfiguration();
		if(c.orientation == Configuration.ORIENTATION_LANDSCAPE){
			tempLL.setOrientation(LinearLayout.HORIZONTAL);
		} else {
			tempLL.setOrientation(LinearLayout.VERTICAL);
			Log.d("Buster", "setting layout vertical");
		}
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.controller);
        setupTopLayout();
        
        videoView = (VideoView)findViewById(R.id.controllerVideoView);
        gd = new GestureDetector(videoView.getContext(), new NavigationOnGestureListener());
        Log.d("Buster", "created gesture listerner = " + gd);
        
        
        finishButton = (Button)findViewById(R.id.finishController);
        finishButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();	
			}
		});
        leftDriveButton = (Button)findViewById(R.id.leftDriveButton);
        leftDriveButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				drive();	
			}
		});
        leftDriveButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				responseView.setText("test button touch");
				return false;
			}
		});
        leftReverseButton = (Button)findViewById(R.id.leftReverseButton);
        leftReverseButton.setOnClickListener(new View.OnClickListener() {
        	
			@Override
			public void onClick(View v) {
				drive();	
			}
		});
        rightDriveButton = (Button)findViewById(R.id.rightDriveButton);
        rightDriveButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				drive();	
			}
		});
        rightReverseButton = (Button)findViewById(R.id.rightReverseButton);
        rightReverseButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				drive();	
			}
		});
        
        responseView = (TextView)findViewById(R.id.controllerResponse);
        		
        mHandler = new Handler(){
    		public void handleMessage(Message msg) {
				responseView.setText(msg.getData().getString("response"));
    		};
    	};
        
        endpoint = new ControllerEndpoint(mHandler);
        eThread = new Thread(endpoint);
        eThread.start();
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		setupTopLayout();
        if(!eThread.isAlive()){
        	eThread.start();
        }
	}

	@Override
	protected void onStop() {
		if(eThread.isAlive()){
			endpoint.stopThread();
		}
		super.onStop();
	}	
	
	private void drive(){
		responseView.setText("test button click");
	}
	
	private void look(float xDelta, float yDelta){
		HashMap<String, Object> look = new HashMap<String, Object>();
		look.put("xDelta", xDelta);
		look.put("yDelta", yDelta);
		Log.d("Buster", "look x = " + xDelta + " y = " + yDelta);
		HashMap<String, Object> request = new HashMap<String, Object>();
		request.put("look", new JSONObject(look));
		endpoint.sendRequest(request);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gd.onTouchEvent(event);
	}
	
}
